<!--
  智慧社区管理系统 - 设备维护
  @author Wu.Liang
  @date 2025-01-20
  @description 设备维护页面，展示设备维护计划、维护记录、维护统计等功能
-->
<template>
  <div class="device-maintenance-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2 class="page-title">
          <el-icon class="title-icon"><Tools /></el-icon>
          设备维护
        </h2>
        <p class="page-description">管理社区设备维护计划，跟踪维护记录，确保设备正常运行</p>
      </div>
      <div class="header-right">
        <el-button type="primary" @click="handleAddMaintenance">
          <el-icon><Plus /></el-icon>
          新增维护计划
        </el-button>
        <el-button type="success" @click="handleMaintenanceStats">
          <el-icon><DataBoard /></el-icon>
          维护统计
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon total">
                <el-icon><Tools /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ maintenanceStats.totalCount }}</div>
                <div class="stats-label">维护计划</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon pending">
                <el-icon><Clock /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ maintenanceStats.pendingCount }}</div>
                <div class="stats-label">待执行</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon inProgress">
                <el-icon><Loading /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ maintenanceStats.inProgressCount }}</div>
                <div class="stats-label">执行中</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon completed">
                <el-icon><Select /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ maintenanceStats.completedCount }}</div>
                <div class="stats-label">已完成</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索和筛选 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="searchForm" inline>
        <el-form-item label="设备类型">
          <el-select v-model="searchForm.deviceType" placeholder="请选择设备类型" clearable style="width: 150px">
            <el-option :label="'门禁设备'" :value="1" />
            <el-option :label="'监控设备'" :value="2" />
            <el-option :label="'照明设备'" :value="3" />
            <el-option :label="'电梯设备'" :value="4" />
            <el-option :label="'消防设备'" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="维护状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 150px">
            <el-option :label="'待执行'" :value="1" />
            <el-option :label="'执行中'" :value="2" />
            <el-option :label="'已完成'" :value="3" />
            <el-option :label="'已取消'" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="维护类型">
          <el-select v-model="searchForm.maintenanceType" placeholder="请选择类型" clearable style="width: 150px">
            <el-option :label="'定期维护'" :value="1" />
            <el-option :label="'故障维修'" :value="2" />
            <el-option :label="'预防性维护'" :value="3" />
            <el-option :label="'紧急维修'" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="维护人员">
          <el-input v-model="searchForm.maintainer" placeholder="请输入维护人员" style="width: 150px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card" shadow="never">
      <template #header>
        <div class="table-header">
          <span class="table-title">维护记录列表</span>
          <div class="table-actions">
            <el-button type="primary" @click="handleExport">
              <el-icon><Download /></el-icon>
              导出
            </el-button>
            <el-button @click="handleRefresh">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="maintenanceList"
        stripe
        border
        :row-class-name="getRowClassName"
        style="width: 100%"
      >
        <el-table-column prop="maintenanceNo" label="维护编号" width="110" />
        <el-table-column prop="deviceName" label="设备名称" width="140" />
        <el-table-column label="设备类型" width="90">
          <template #default="{ row }">
            <el-tag :type="getDeviceTypeTagType(row.deviceType)" size="small">
              {{ getDeviceTypeName(row.deviceType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="维护类型" width="90">
          <template #default="{ row }">
            <el-tag :type="getMaintenanceTypeTagType(row.maintenanceType)" size="small">
              {{ getMaintenanceTypeName(row.maintenanceType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="maintainer" label="维护人员" width="90" />
        <el-table-column prop="plannedDate" label="计划日期" width="110" />
        <el-table-column label="状态" width="90">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)" size="small">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="actualDate" label="实际日期" width="110" />
        <el-table-column prop="cost" label="费用(元)" width="80" />
        <el-table-column prop="content" label="维护内容" min-width="180" show-overflow-tooltip />
        <el-table-column prop="result" label="维护结果" min-width="120" show-overflow-tooltip />
        <el-table-column prop="remark" label="备注" min-width="100" show-overflow-tooltip />
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="{ row }">
            <el-button type="primary" link size="small" @click="handleDetail(row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
            <el-button type="primary" link size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button type="danger" link size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="维护详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <div v-if="currentMaintenance" class="detail-content">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="维护编号">{{ currentMaintenance.maintenanceNo }}</el-descriptions-item>
          <el-descriptions-item label="设备名称">{{ currentMaintenance.deviceName }}</el-descriptions-item>
          <el-descriptions-item label="设备类型">
            <el-tag :type="getDeviceTypeTagType(currentMaintenance.deviceType)" size="small">
              {{ getDeviceTypeName(currentMaintenance.deviceType) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="维护类型">
            <el-tag :type="getMaintenanceTypeTagType(currentMaintenance.maintenanceType)" size="small">
              {{ getMaintenanceTypeName(currentMaintenance.maintenanceType) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="维护人员">{{ currentMaintenance.maintainer }}</el-descriptions-item>
          <el-descriptions-item label="计划日期">{{ currentMaintenance.plannedDate }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="getStatusTagType(currentMaintenance.status)" size="small">
              {{ getStatusName(currentMaintenance.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="实际日期">{{ currentMaintenance.actualDate || '-' }}</el-descriptions-item>
          <el-descriptions-item label="费用">{{ currentMaintenance.cost }}元</el-descriptions-item>
          <el-descriptions-item label="维护内容" :span="2">{{ currentMaintenance.content }}</el-descriptions-item>
          <el-descriptions-item label="维护结果" :span="2">{{ currentMaintenance.result || '-' }}</el-descriptions-item>
          <el-descriptions-item label="备注" :span="2">{{ currentMaintenance.remark }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleEditMaintenance">编辑</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  Tools,
  Plus,
  DataBoard,
  Clock,
  Loading,
  Select,
  Search,
  Refresh,
  Download,
  View,
  Edit,
  Delete
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const detailDialogVisible = ref(false)
const currentMaintenance = ref<any>(null)

// 搜索表单
const searchForm = reactive({
  deviceType: undefined as number | undefined,
  status: undefined as number | undefined,
  maintenanceType: undefined as number | undefined,
  maintainer: ''
})

// 分页信息
const pagination = reactive({
  current: 1,
  size: 20,
  total: 0
})

// 维护统计数据
const maintenanceStats = reactive({
  totalCount: 45,
  pendingCount: 12,
  inProgressCount: 8,
  completedCount: 25
})

// 维护记录列表数据
const maintenanceList = ref([
  {
    id: 1,
    maintenanceNo: 'WH202501001',
    deviceName: '1号楼门禁',
    deviceType: 1,
    maintenanceType: 1,
    maintainer: '张师傅',
    plannedDate: '2025-01-25',
    status: 1,
    actualDate: null,
    cost: 200,
    content: '定期检查门禁系统，清洁读卡器，测试开关门功能',
    result: null,
    remark: '正常维护'
  },
  {
    id: 2,
    maintenanceNo: 'WH202501002',
    deviceName: '社区监控摄像头',
    deviceType: 2,
    maintenanceType: 2,
    maintainer: '李师傅',
    plannedDate: '2025-01-26',
    status: 2,
    actualDate: '2025-01-26',
    cost: 500,
    content: '摄像头故障维修，更换镜头组件',
    result: '维修完成，设备恢复正常',
    remark: '紧急维修'
  },
  {
    id: 3,
    maintenanceNo: 'WH202501003',
    deviceName: '地下车库照明',
    deviceType: 3,
    maintenanceType: 3,
    maintainer: '王师傅',
    plannedDate: '2025-01-28',
    status: 3,
    actualDate: '2025-01-28',
    cost: 300,
    content: '预防性维护，检查照明线路，更换老化灯具',
    result: '维护完成，照明效果良好',
    remark: '预防性维护'
  },
  {
    id: 4,
    maintenanceNo: 'WH202501004',
    deviceName: '2号楼电梯',
    deviceType: 4,
    maintenanceType: 1,
    maintainer: '赵师傅',
    plannedDate: '2025-01-30',
    status: 1,
    actualDate: null,
    cost: 800,
    content: '定期维护，检查电梯运行状态，润滑系统',
    result: null,
    remark: '定期维护'
  },
  {
    id: 5,
    maintenanceNo: 'WH202501005',
    deviceName: '消防报警器',
    deviceType: 5,
    maintenanceType: 4,
    maintainer: '孙师傅',
    plannedDate: '2025-01-22',
    status: 3,
    actualDate: '2025-01-22',
    cost: 150,
    content: '紧急维修，修复误报问题',
    result: '维修完成，误报问题解决',
    remark: '紧急维修'
  }
])

// 计算属性
const getDeviceTypeTagType = (type: number): 'primary' | 'success' | 'warning' | 'info' | 'danger' => {
  const typeMap: Record<number, 'primary' | 'success' | 'warning' | 'info' | 'danger'> = {
    1: 'primary',
    2: 'success',
    3: 'warning',
    4: 'info',
    5: 'danger'
  }
  return typeMap[type] || 'info'
}

const getDeviceTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '门禁设备',
    2: '监控设备',
    3: '照明设备',
    4: '电梯设备',
    5: '消防设备'
  }
  return typeMap[type] || '未知类型'
}

const getMaintenanceTypeTagType = (type: number): 'primary' | 'success' | 'warning' | 'info' | 'danger' => {
  const typeMap: Record<number, 'primary' | 'success' | 'warning' | 'info' | 'danger'> = {
    1: 'primary',
    2: 'danger',
    3: 'warning',
    4: 'danger'
  }
  return typeMap[type] || 'info'
}

const getMaintenanceTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '定期维护',
    2: '故障维修',
    3: '预防性维护',
    4: '紧急维修'
  }
  return typeMap[type] || '未知类型'
}

const getStatusTagType = (status: number): 'primary' | 'success' | 'warning' | 'info' | 'danger' => {
  const statusMap: Record<number, 'primary' | 'success' | 'warning' | 'info' | 'danger'> = {
    1: 'warning',
    2: 'primary',
    3: 'success',
    0: 'info'
  }
  return statusMap[status] || 'info'
}

const getStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    1: '待执行',
    2: '执行中',
    3: '已完成',
    0: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 方法
const handleSearch = () => {
  pagination.current = 1
  loadMaintenanceList()
}

const handleReset = () => {
  searchForm.deviceType = undefined
  searchForm.status = undefined
  searchForm.maintenanceType = undefined
  searchForm.maintainer = ''
  pagination.current = 1
  loadMaintenanceList()
}

const handleAddMaintenance = () => {
  showMessage({ type: 'info', message: '新增维护计划功能开发中' })
}

const handleMaintenanceStats = () => {
  showMessage({ type: 'info', message: '维护统计功能开发中' })
}

const handleExport = () => {
  showMessage({ type: 'info', message: '导出功能开发中' })
}

const handleRefresh = () => {
  loadMaintenanceList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

const handleDetail = (row: any) => {
  currentMaintenance.value = row
  detailDialogVisible.value = true
}

const handleEdit = (row: any) => {
  showMessage({ type: 'info', message: `编辑维护计划功能开发中 - ${row.maintenanceNo}` })
}

const handleDelete = (row: any) => {
  showMessage({ type: 'info', message: `删除维护计划功能开发中 - ${row.maintenanceNo}` })
}

const handleEditMaintenance = () => {
  showMessage({ type: 'info', message: '编辑维护功能开发中' })
}

const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.current = 1
  loadMaintenanceList()
}

const handleCurrentChange = (current: number) => {
  pagination.current = current
  loadMaintenanceList()
}

const getRowClassName = ({ row }: { row: any }) => {
  if (row.status === 2) return 'in-progress-row'
  if (row.status === 0) return 'cancelled-row'
  return ''
}

const loadMaintenanceList = () => {
  loading.value = true
  // 模拟加载数据
  setTimeout(() => {
    loading.value = false
    pagination.total = maintenanceList.value.length
  }, 500)
}

// 生命周期
onMounted(() => {
  loadMaintenanceList()
})
</script>

<style scoped lang="scss">
.device-maintenance-container {
  padding: 24px;

  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 24px;

    .header-left {
      .page-title {
        display: flex;
        align-items: center;
        gap: 12px;
        font-size: 24px;
        font-weight: 600;
        color: var(--text-primary);
        margin: 0 0 8px 0;

        .title-icon {
          color: var(--primary-color);
        }
      }

      .page-description {
        font-size: 14px;
        color: var(--text-secondary);
        margin: 0;
      }
    }

    .header-right {
      display: flex;
      gap: 12px;
    }
  }

  .stats-cards {
    margin-bottom: 24px;

    .stats-card {
      border: none;
      border-radius: 12px;

      .stats-content {
        display: flex;
        align-items: center;
        gap: 16px;

        .stats-icon {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 24px;
          color: white;

          &.total {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          }

          &.pending {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
          }

          &.inProgress {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
          }

          &.completed {
            background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
          }
        }

        .stats-info {
          .stats-number {
            font-size: 24px;
            font-weight: 600;
            color: var(--text-primary);
            line-height: 1;
          }

          .stats-label {
            font-size: 14px;
            color: var(--text-secondary);
            margin-top: 4px;
          }
        }
      }
    }
  }

  .search-card {
    margin-bottom: 24px;
    border-radius: 12px;
  }

  .table-card {
    border-radius: 12px;

    .table-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title {
        font-size: 16px;
        font-weight: 600;
        color: var(--text-primary);
      }

      .table-actions {
        display: flex;
        gap: 12px;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 24px;
    }
  }

  .detail-content {
    .el-descriptions {
      margin-bottom: 20px;
    }
  }

  // 表格行样式
  :deep(.in-progress-row) {
    background-color: #f0f9ff !important;
  }

  :deep(.cancelled-row) {
    background-color: #fef2f2 !important;
  }

  // 操作按钮样式优化
  :deep(.el-table .cell) {
    .el-button--small {
      padding: 4px 8px;
      font-size: 12px;
      margin: 0 2px;
    }
  }

  // 操作列样式
  :deep(.el-table .el-table__cell:last-child) {
    .cell {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 4px;
    }
  }
}
</style> 